package com.sky.baidu;


import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.OrderBusinessException;
import com.sky.service.impl.AddressBookServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class BaiDuAPIServiceImpl implements BaiDuAPIService{

    @Autowired
    private BaiDuUtil baiduUtil;

    @Autowired
    private AddressBookServiceImpl addressBookService;

    @Value("${sky.shop}")
    private String shop;

    @Override
    public void compute(String addressBook) {
        try {
            // 假设商家坐标存储在配置或数据库中
            // 调用百度地图API获取商家坐标经纬度
            String originLatLng = baiduUtil.parseLocation(shop);

            // 调用百度地图API获取收货地址坐标经纬度
            String deliveryLatLng = baiduUtil.parseLocation(addressBook);

            // 调用百度地图API获取商家和收货地址之间的距离
            Integer distance = baiduUtil.parseDistance(originLatLng,deliveryLatLng);
            System.out.println("商家和用户距离："+distance);

            if (distance == -1){
                throw new AddressBookBusinessException("地址解析失败");
            }else if (distance > 5000) {
                throw new OrderBusinessException("超出配送范围");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}